import { createRouter, createWebHistory } from 'vue-router'
import RegiestView from '@/views/Regiest/index.vue'
import LoginView from '@/views/Login/index.vue'
import MydataView from '@/views/Mydata/index.vue'
import HomeView from '@/views/Home/index.vue'
import GoodsView from '@/views/goods/index.vue'
import IndexView from '@/views/Home/home.vue'
import ClassView from '@/views/Home/class.vue'
import CartView from '@/views/Home/cart.vue'
import MyView from '@/views/Home/my.vue'
import SousuoView from '@/views/sousuo/index.vue'
import XiangqingView from '@/views/xiangqing/index.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/', redirect: '/home/home' },
    {
      path: '/regiest',
      component: RegiestView,
    },
     {
      path: '/login',
      component: LoginView,
    },
      {
      path: '/mydata',
      component: MydataView,
    },
     {
      path: '/home',
      component: HomeView,
         children:[
           {
               path: 'home',
               component: IndexView,
            },
             {
               path: 'class',
               component: ClassView,
            },
             {
               path: 'cart',
               component: CartView,
            },
             {
               path: 'my',
               component: MyView,
            },
         ]
    },
     {
      path: '/sousuo',
      component: SousuoView,
    },
     {
      path: '/xiangqing/:goods_id',
      component: XiangqingView,
    },
     {
      path: '/goods/:cat_name',
      component: GoodsView,
    },
  ],
})
// 导航守卫
router.beforeEach((to, from,next) => {
   if(to.fullPath=='/home/cart')
   {
      let token=localStorage.getItem('token')
       if(token)
       {
          next()
       }
       else
       {
         next('/login')
       }
   }
   else
   {
     next()
   }
  return false
})
export default router
